"use client"

import { useState } from "react"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Textarea } from "@/components/ui/textarea"
import { Label } from "@/components/ui/label"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select"
import {
  Tabs,
  TabsContent,
  TabsList,
  TabsTrigger,
} from "@/components/ui/tabs"
import {
  Settings,
  Zap,
  Thermometer,
  Shield,
  Power,
  RotateCcw,
  FileText,
  Calendar,
  CheckCircle,
  Clock,
  Activity,
  AlertTriangle,
  Badge
} from "lucide-react"

interface ProjectFormData {
  // 基础信息
  PROJECT_CODE: string
  PROJECT_TYPE_NAME: string
  customerName: string
  salespersonName: string
  PROJECT_PLAN_START_DATE: string
  PROJECT_PLAN_END_DATE: string
  PROJECT_DESC: string
  requirements: string
  
  // 基本参数
  WORK_TEMP: string
  CERTIFICATION: string
  LOW_POWER: string
  STARTUP_TIME: string
  CONFORMAL_COATING: string
  RATED_VOLTAGE: string
  MAX_POWER: string
  MAX_SPEED: string
  
  // 控制详细描述
  controlDescription1: string
  controlDescription2: string
  controlDescription3: string
  controlDescription4: string
  
  // 电机参数
  motorType: string
  motorPower: string
  motorVoltage: string
  motorCurrent: string
  ratedSpeed: string
  ratedTorque: string
  polePairs: string
  phases: string
  
  // 控制参数
  CONTROL_MODE: string
  CONTROL_ALGORITHM: string
  HAS_HALL: string
  
  // 保护参数
  STALL_PROTECTION: string
  OVERVOLTAGE_PROTECTION: string
  UNDERVOLTAGE_PROTECTION: string
  TEMPERATURE_PROTECTION: string
  OVERCURRENT_PROTECTION: string
  CURRENT_LIMIT_PROTECTION: string
  BLOCKAGE_PROTECTION: string
  PHASE_PROTECTION: string
  
  // 保护详细参数
  STALL_HOLD_TIME: string
  STALL_RESTART_TIME: string
  STALL_RESTART_COUNT: string
  OVERVOLTAGE_VALUE: string
  OVERVOLTAGE_RECOVERY_VALUE: string
  UNDERVOLTAGE_VALUE: string
  UNDERVOLTAGE_RECOVERY_VALUE: string
  protectionTemp: string
  recoveryTemp: string
  overcurrentValue: string
  currentLimitValue: string
  blockageDiameter: string
  blockageHoldTime: string
}

interface ProjectCreateFormProps {
  onCreateProject?: (data: ProjectFormData) => void
}

export default function ProjectCreateForm({ onCreateProject }: ProjectCreateFormProps) {
  const [loading, setLoading] = useState(false)
  const [errors, setErrors] = useState<Record<string, string>>({})
  const [activeTab, setActiveTab] = useState("basic")
  
  const [formData, setFormData] = useState<ProjectFormData>({
    PROJECT_CODE: "",
    PROJECT_TYPE_NAME: "",
    customerName: "",
    salespersonName: "",
    PROJECT_PLAN_START_DATE: "",
    PROJECT_PLAN_END_DATE: "",
    PROJECT_DESC: "",
    requirements: "",
    WORK_TEMP: "",
    CERTIFICATION: "",
    LOW_POWER: "",
    STARTUP_TIME: "",
    CONFORMAL_COATING: "",
    RATED_VOLTAGE: "",
    MAX_POWER: "",
    MAX_SPEED: "",
    controlDescription1: "",
    controlDescription2: "",
    controlDescription3: "",
    controlDescription4: "",
    motorType: "",
    motorPower: "",
    motorVoltage: "",
    motorCurrent: "",
    ratedSpeed: "",
    ratedTorque: "",
    polePairs: "",
    phases: "",
    CONTROL_MODE: "",
    CONTROL_ALGORITHM: "",
    HAS_HALL: "",
    STALL_PROTECTION: "",
    OVERVOLTAGE_PROTECTION: "",
    UNDERVOLTAGE_PROTECTION: "",
    TEMPERATURE_PROTECTION: "",
    OVERCURRENT_PROTECTION: "",
    CURRENT_LIMIT_PROTECTION: "",
    BLOCKAGE_PROTECTION: "",
    PHASE_PROTECTION: "",
    STALL_HOLD_TIME: "",
    STALL_RESTART_TIME: "",
    STALL_RESTART_COUNT: "",
    OVERVOLTAGE_VALUE: "",
    OVERVOLTAGE_RECOVERY_VALUE: "",
    UNDERVOLTAGE_VALUE: "",
    UNDERVOLTAGE_RECOVERY_VALUE: "",
    protectionTemp: "",
    recoveryTemp: "",
    overcurrentValue: "",
    currentLimitValue: "",
    blockageDiameter: "",
    blockageHoldTime: ""
  })

  const handleInputChange = (field: keyof ProjectFormData, value: string) => {
    setFormData(prev => ({ ...prev, [field]: value }))
    if (errors[field]) {
      setErrors(prev => ({ ...prev, [field]: "" }))
    }
  }

  const validateForm = () => {
    const newErrors: Record<string, string> = {}
    
    if (!formData.PROJECT_CODE.trim()) {
      newErrors.PROJECT_CODE = "项目编号不能为空"
    }
    
    if (!formData.PROJECT_TYPE_NAME.trim()) {
      newErrors.PROJECT_TYPE_NAME = "项目类型不能为空"
    }
    
    if (!formData.customerName.trim()) {
      newErrors.customerName = "客户名称不能为空"
    }
    
    if (!formData.motorType.trim()) {
      newErrors.motorType = "电机类型不能为空"
    }
    
    if (!formData.motorPower.trim()) {
      newErrors.motorPower = "电机功率不能为空"
    }
    
    if (!formData.motorVoltage.trim()) {
      newErrors.motorVoltage = "电机电压不能为空"
    }
    
    if (!formData.motorCurrent.trim()) {
      newErrors.motorCurrent = "电机电流不能为空"
    }
    
    setErrors(newErrors)
    return Object.keys(newErrors).length === 0
  }

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault()
    
    if (!validateForm()) {
      return
    }
    
    setLoading(true)
    
    try {
      const response = await fetch('/api/motor-projects', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(formData)
      })
      
      if (response.ok) {
        const createdProject = await response.json()
        
        if (onCreateProject) {
          onCreateProject(createdProject)
        }
        
        // 重置表单
        setFormData({
          PROJECT_CODE: "",
          PROJECT_TYPE_NAME: "",
          customerName: "",
          salespersonName: "",
          PROJECT_PLAN_START_DATE: "",
          PROJECT_PLAN_END_DATE: "",
          PROJECT_DESC: "",
          requirements: "",
          WORK_TEMP: "",
          CERTIFICATION: "",
          LOW_POWER: "",
          STARTUP_TIME: "",
          CONFORMAL_COATING: "",
          RATED_VOLTAGE: "",
          MAX_POWER: "",
          MAX_SPEED: "",
          controlDescription1: "",
          controlDescription2: "",
          controlDescription3: "",
          controlDescription4: "",
          motorType: "",
          motorPower: "",
          motorVoltage: "",
          motorCurrent: "",
          ratedSpeed: "",
          ratedTorque: "",
          polePairs: "",
          phases: "",
          CONTROL_MODE: "",
          CONTROL_ALGORITHM: "",
          HAS_HALL: "",
          STALL_PROTECTION: "",
          OVERVOLTAGE_PROTECTION: "",
          UNDERVOLTAGE_PROTECTION: "",
          TEMPERATURE_PROTECTION: "",
          OVERCURRENT_PROTECTION: "",
          CURRENT_LIMIT_PROTECTION: "",
          BLOCKAGE_PROTECTION: "",
          PHASE_PROTECTION: "",
          STALL_HOLD_TIME: "",
          STALL_RESTART_TIME: "",
          STALL_RESTART_COUNT: "",
          OVERVOLTAGE_VALUE: "",
          OVERVOLTAGE_RECOVERY_VALUE: "",
          UNDERVOLTAGE_VALUE: "",
          UNDERVOLTAGE_RECOVERY_VALUE: "",
          protectionTemp: "",
          recoveryTemp: "",
          overcurrentValue: "",
          currentLimitValue: "",
          blockageDiameter: "",
          blockageHoldTime: ""
        })
      } else {
        const errorData = await response.json()
        setErrors({ submit: errorData.error || '创建项目时发生错误' })
      }
    } catch (error) {
      console.error('Error creating project:', error)
      setErrors({ submit: '创建项目时发生错误' })
    } finally {
      setLoading(false)
    }
  }

  return (
    <div className="w-full max-w-4xl mx-auto">
      <div className="mb-6">
        <h1 className="text-2xl font-bold text-gray-900">创建电机控制项目</h1>
        <p className="text-gray-600 mt-2">带*字段为必填项</p>
      </div>
      
      <form onSubmit={handleSubmit} className="space-y-6">
        <Tabs value={activeTab} onValueChange={setActiveTab} className="w-full">
          <TabsList className="grid w-full grid-cols-4">
            <TabsTrigger value="basic">基础信息</TabsTrigger>
            <TabsTrigger value="parameters">基本参数</TabsTrigger>
            <TabsTrigger value="motor">电机参数</TabsTrigger>
            <TabsTrigger value="protection">保护设置</TabsTrigger>
          </TabsList>
          
          <TabsContent value="basic" className="space-y-4">
            <Card>
              <CardHeader>
                <CardTitle className="text-lg flex items-center gap-2">
                  <FileText className="h-5 w-5" />
                  基础信息
                </CardTitle>
              </CardHeader>
              <CardContent className="space-y-4">
                <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                  <div className="space-y-2">
                    <Label className="text-sm">项目编号 *</Label>
                    <Input
                      value={formData.PROJECT_CODE}
                      onChange={(e) => handleInputChange("PROJECT_CODE", e.target.value)}
                      placeholder="PROJ-2024-001"
                    />
                    {errors.PROJECT_CODE && <p className="text-red-500 text-xs">{errors.PROJECT_CODE}</p>}
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">项目类型 *</Label>
                    <Select value={formData.PROJECT_TYPE_NAME} onValueChange={(value) => handleInputChange("PROJECT_TYPE_NAME", value)}>
                      <SelectTrigger>
                        <SelectValue placeholder="选择项目类型" />
                      </SelectTrigger>
                      <SelectContent>
                        <SelectItem value="风机">风机</SelectItem>
                        <SelectItem value="霍尔">霍尔</SelectItem>
                        <SelectItem value="磁编">磁编</SelectItem>
                        <SelectItem value="吸尘器">吸尘器</SelectItem>
                        <SelectItem value="泵">泵</SelectItem>
                      </SelectContent>
                    </Select>
                    {errors.PROJECT_TYPE_NAME && <p className="text-red-500 text-xs">{errors.PROJECT_TYPE_NAME}</p>}
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">客户名称 *</Label>
                    <Input
                      value={formData.customerName}
                      onChange={(e) => handleInputChange("customerName", e.target.value)}
                      placeholder="输入客户名称"
                    />
                    {errors.customerName && <p className="text-red-500 text-xs">{errors.customerName}</p>}
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">业务员</Label>
                    <Input
                      value={formData.salespersonName}
                      onChange={(e) => handleInputChange("salespersonName", e.target.value)}
                      placeholder="输入业务员姓名"
                    />
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">开始时间</Label>
                    <Input
                      type="date"
                      value={formData.PROJECT_PLAN_START_DATE}
                      onChange={(e) => handleInputChange("PROJECT_PLAN_START_DATE", e.target.value)}
                    />
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">截止时间</Label>
                    <Input
                      type="date"
                      value={formData.PROJECT_PLAN_END_DATE}
                      onChange={(e) => handleInputChange("PROJECT_PLAN_END_DATE", e.target.value)}
                    />
                  </div>
                </div>
                
                <div className="space-y-2">
                  <Label className="text-sm">项目描述</Label>
                  <Textarea
                    value={formData.PROJECT_DESC}
                    onChange={(e) => handleInputChange("PROJECT_DESC", e.target.value)}
                    placeholder="详细描述项目信息"
                    rows={4}
                  />
                </div>
                
                <div className="space-y-2">
                  <Label className="text-sm">需求描述</Label>
                  <Textarea
                    value={formData.requirements}
                    onChange={(e) => handleInputChange("requirements", e.target.value)}
                    placeholder="描述项目需求"
                    rows={3}
                  />
                </div>
              </CardContent>
            </Card>
          </TabsContent>
          
          <TabsContent value="parameters" className="space-y-4">
            <Card>
              <CardHeader>
                <CardTitle className="text-lg flex items-center gap-2">
                  <Settings className="h-5 w-5" />
                  基本参数
                </CardTitle>
              </CardHeader>
              <CardContent>
                <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                  <div className="space-y-2">
                    <Label className="text-sm">工作环境温度 (°C)</Label>
                    <Input
                      value={formData.WORK_TEMP}
                      onChange={(e) => handleInputChange("WORK_TEMP", e.target.value)}
                      placeholder="-10度 +50度"
                    />
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">认证要求</Label>
                    <Input
                      value={formData.CERTIFICATION}
                      onChange={(e) => handleInputChange("CERTIFICATION", e.target.value)}
                      placeholder="CE, FCC, RoHS"
                    />
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">低功耗要求 (μA)</Label>
                    <Input
                      value={formData.LOW_POWER}
                      onChange={(e) => handleInputChange("LOW_POWER", e.target.value)}
                      placeholder="100"
                    />
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">启动时间 (S)</Label>
                    <Input
                      value={formData.STARTUP_TIME}
                      onChange={(e) => handleInputChange("STARTUP_TIME", e.target.value)}
                      placeholder="1.0"
                    />
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">三防漆要求</Label>
                    <Select value={formData.CONFORMAL_COATING} onValueChange={(value) => handleInputChange("CONFORMAL_COATING", value)}>
                      <SelectTrigger>
                        <SelectValue placeholder="选择三防漆要求" />
                      </SelectTrigger>
                      <SelectContent>
                        <SelectItem value="有">有</SelectItem>
                        <SelectItem value="无">无</SelectItem>
                      </SelectContent>
                    </Select>
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">额定电压 (V)</Label>
                    <Input
                      value={formData.RATED_VOLTAGE}
                      onChange={(e) => handleInputChange("RATED_VOLTAGE", e.target.value)}
                      placeholder="24"
                    />
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">最大功率 (W)</Label>
                    <Input
                      value={formData.MAX_POWER}
                      onChange={(e) => handleInputChange("MAX_POWER", e.target.value)}
                      placeholder="500"
                    />
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">最高转速 (RPM)</Label>
                    <Input
                      value={formData.MAX_SPEED}
                      onChange={(e) => handleInputChange("MAX_SPEED", e.target.value)}
                      placeholder="3000"
                    />
                  </div>
                </div>
              </CardContent>
            </Card>

            <Card>
              <CardHeader>
                <CardTitle className="text-lg flex items-center gap-2">
                  <Settings className="h-5 w-5" />
                  控制详细描述
                </CardTitle>
              </CardHeader>
              <CardContent className="space-y-4">
                <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                  <div className="space-y-2">
                    <Label className="text-sm">描述 1</Label>
                    <Textarea
                      value={formData.controlDescription1}
                      onChange={(e) => handleInputChange("controlDescription1", e.target.value)}
                      placeholder="启动控制：采用软启动方式，启动时间可调，避免电流冲击"
                      rows={2}
                    />
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">描述 2</Label>
                    <Textarea
                      value={formData.controlDescription2}
                      onChange={(e) => handleInputChange("controlDescription2", e.target.value)}
                      placeholder="运行控制：支持多种控制模式切换，实时监控运行状态"
                      rows={2}
                    />
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">描述 3</Label>
                    <Textarea
                      value={formData.controlDescription3}
                      onChange={(e) => handleInputChange("controlDescription3", e.target.value)}
                      placeholder="保护控制：多重保护机制，确保设备安全运行"
                      rows={2}
                    />
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">描述 4</Label>
                    <Textarea
                      value={formData.controlDescription4}
                      onChange={(e) => handleInputChange("controlDescription4", e.target.value)}
                      placeholder="通信控制：支持远程控制和状态监控，便于系统集成"
                      rows={2}
                    />
                  </div>
                </div>
              </CardContent>
            </Card>
          </TabsContent>
          
          <TabsContent value="motor" className="space-y-4">
            <Card>
              <CardHeader>
                <CardTitle className="text-lg flex items-center gap-2">
                  <Zap className="h-5 w-5" />
                  电机参数
                </CardTitle>
              </CardHeader>
              <CardContent>
                <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
                  <div className="space-y-2">
                    <Label className="text-sm">电机类型 *</Label>
                    <Select value={formData.motorType} onValueChange={(value) => handleInputChange("motorType", value)}>
                      <SelectTrigger>
                        <SelectValue placeholder="选择电机类型" />
                      </SelectTrigger>
                      <SelectContent>
                        <SelectItem value="无刷电机">无刷电机</SelectItem>
                        <SelectItem value="有刷电机">有刷电机</SelectItem>
                        <SelectItem value="步进电机">步进电机</SelectItem>
                        <SelectItem value="伺服电机">伺服电机</SelectItem>
                      </SelectContent>
                    </Select>
                    {errors.motorType && <p className="text-red-500 text-xs">{errors.motorType}</p>}
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">电机功率 (W) *</Label>
                    <Input
                      type="number"
                      value={formData.motorPower}
                      onChange={(e) => handleInputChange("motorPower", e.target.value)}
                      placeholder="500"
                    />
                    {errors.motorPower && <p className="text-red-500 text-xs">{errors.motorPower}</p>}
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">电机电压 (V) *</Label>
                    <Input
                      type="number"
                      value={formData.motorVoltage}
                      onChange={(e) => handleInputChange("motorVoltage", e.target.value)}
                      placeholder="24"
                    />
                    {errors.motorVoltage && <p className="text-red-500 text-xs">{errors.motorVoltage}</p>}
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">电机电流 (A) *</Label>
                    <Input
                      type="number"
                      value={formData.motorCurrent}
                      onChange={(e) => handleInputChange("motorCurrent", e.target.value)}
                      placeholder="10"
                    />
                    {errors.motorCurrent && <p className="text-red-500 text-xs">{errors.motorCurrent}</p>}
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">额定转速 (rpm)</Label>
                    <Input
                      type="number"
                      value={formData.ratedSpeed}
                      onChange={(e) => handleInputChange("ratedSpeed", e.target.value)}
                      placeholder="3000"
                    />
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">额定扭矩 (Nm)</Label>
                    <Input
                      type="number"
                      step="0.1"
                      value={formData.ratedTorque}
                      onChange={(e) => handleInputChange("ratedTorque", e.target.value)}
                      placeholder="5.0"
                    />
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">极对数</Label>
                    <Input
                      type="number"
                      value={formData.polePairs}
                      onChange={(e) => handleInputChange("polePairs", e.target.value)}
                      placeholder="4"
                    />
                  </div>
                  
                  <div className="space-y-2">
                    <Label className="text-sm">相数</Label>
                    <Input
                      type="number"
                      value={formData.phases}
                      onChange={(e) => handleInputChange("phases", e.target.value)}
                      placeholder="3"
                    />
                  </div>
                </div>
              </CardContent>
            </Card>

            <Card>
              <CardHeader>
                <CardTitle className="text-lg flex items-center gap-2">
                  <Settings className="h-5 w-5" />
                  控制参数
                </CardTitle>
              </CardHeader>
              <CardContent>
                <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
                  <div className="space-y-2">
                    <Label className="text-sm">控制模式</Label>
                    <Select value={formData.CONTROL_MODE} onValueChange={(value) => handleInputChange("CONTROL_MODE", value)}>
                      <SelectTrigger>
                        <SelectValue placeholder="选择控制模式" />
                      </SelectTrigger>
                      <SelectContent>
                        <SelectItem value="PWM控制">PWM控制</SelectItem>
                        <SelectItem value="电压控制">电压控制</SelectItem>
                        <SelectItem value="电流控制">电流控制</SelectItem>
                        <SelectItem value="速度控制">速度控制</SelectItem>
                      </SelectContent>
                    </Select>
                  </div>

                  <div className="space-y-2">
                    <Label className="text-sm">控制算法</Label>
                    <Select value={formData.CONTROL_ALGORITHM} onValueChange={(value) => handleInputChange("CONTROL_ALGORITHM", value)}>
                      <SelectTrigger>
                        <SelectValue placeholder="选择控制算法" />
                      </SelectTrigger>
                      <SelectContent>
                        <SelectItem value="FOC算法">FOC算法</SelectItem>
                        <SelectItem value="PID控制">PID控制</SelectItem>
                        <SelectItem value="矢量控制">矢量控制</SelectItem>
                        <SelectItem value="直接转矩控制">直接转矩控制</SelectItem>
                      </SelectContent>
                    </Select>
                  </div>

                  <div className="space-y-2">
                    <Label className="text-sm">霍尔传感器</Label>
                    <Select value={formData.HAS_HALL} onValueChange={(value) => handleInputChange("HAS_HALL", value)}>
                      <SelectTrigger>
                        <SelectValue placeholder="选择霍尔传感器" />
                      </SelectTrigger>
                      <SelectContent>
                        <SelectItem value="有">有</SelectItem>
                        <SelectItem value="无">无</SelectItem>
                      </SelectContent>
                    </Select>
                  </div>
                </div>
              </CardContent>
            </Card>
          </TabsContent>
          
          <TabsContent value="protection" className="space-y-4">
            <Card>
              <CardHeader>
                <CardTitle className="text-lg flex items-center gap-2">
                  <Shield className="h-5 w-5" />
                  保护参数
                </CardTitle>
              </CardHeader>
              <CardContent className="space-y-6">
                <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                  {/* 堵转保护 */}
                  <div className="space-y-4">
                    <div className="space-y-2">
                      <Label className="text-sm flex items-center">
                        <Zap className="h-4 w-4 mr-1" />
                        堵转保护
                      </Label>
                      <Select value={formData.STALL_PROTECTION} onValueChange={(value) => handleInputChange("STALL_PROTECTION", value)}>
                        <SelectTrigger>
                          <SelectValue placeholder="选择堵转保护" />
                        </SelectTrigger>
                        <SelectContent>
                          <SelectItem value="有，自恢复">有，自恢复</SelectItem>
                          <SelectItem value="有，仅断电重启">有，仅断电重启</SelectItem>
                          <SelectItem value="无">无</SelectItem>
                        </SelectContent>
                      </Select>
                    </div>
                    
                    {formData.STALL_PROTECTION && formData.STALL_PROTECTION !== "无" && (
                      <div className="space-y-2 ml-4">
                        <Input
                          placeholder="保持时间 (S)"
                          value={formData.STALL_HOLD_TIME}
                          onChange={(e) => handleInputChange("STALL_HOLD_TIME", e.target.value)}
                        />
                        <Input
                          placeholder="重启时间 (S)"
                          value={formData.STALL_RESTART_TIME}
                          onChange={(e) => handleInputChange("STALL_RESTART_TIME", e.target.value)}
                        />
                        <Input
                          placeholder="重启次数"
                          value={formData.STALL_RESTART_COUNT}
                          onChange={(e) => handleInputChange("STALL_RESTART_COUNT", e.target.value)}
                        />
                      </div>
                    )}
                  </div>

                  {/* 过压保护 */}
                  <div className="space-y-4">
                    <div className="space-y-2">
                      <Label className="text-sm flex items-center">
                        <Zap className="h-4 w-4 mr-1" />
                        过压保护
                      </Label>
                      <Select value={formData.OVERVOLTAGE_PROTECTION} onValueChange={(value) => handleInputChange("OVERVOLTAGE_PROTECTION", value)}>
                        <SelectTrigger>
                          <SelectValue placeholder="选择过压保护" />
                        </SelectTrigger>
                        <SelectContent>
                          <SelectItem value="有，自恢复">有，自恢复</SelectItem>
                          <SelectItem value="有，仅断电重启">有，仅断电重启</SelectItem>
                          <SelectItem value="无">无</SelectItem>
                        </SelectContent>
                      </Select>
                    </div>
                    
                    {formData.OVERVOLTAGE_PROTECTION && formData.OVERVOLTAGE_PROTECTION !== "无" && (
                      <div className="space-y-2 ml-4">
                        <Input
                          placeholder="保护值 (V)"
                          value={formData.OVERVOLTAGE_VALUE}
                          onChange={(e) => handleInputChange("OVERVOLTAGE_VALUE", e.target.value)}
                        />
                        <Input
                          placeholder="恢复值 (V)"
                          value={formData.OVERVOLTAGE_RECOVERY_VALUE}
                          onChange={(e) => handleInputChange("OVERVOLTAGE_RECOVERY_VALUE", e.target.value)}
                        />
                      </div>
                    )}
                  </div>

                  {/* 欠压保护 */}
                  <div className="space-y-4">
                    <div className="space-y-2">
                      <Label className="text-sm flex items-center">
                        <Zap className="h-4 w-4 mr-1" />
                        欠压保护
                      </Label>
                      <Select value={formData.UNDERVOLTAGE_PROTECTION} onValueChange={(value) => handleInputChange("UNDERVOLTAGE_PROTECTION", value)}>
                        <SelectTrigger>
                          <SelectValue placeholder="选择欠压保护" />
                        </SelectTrigger>
                        <SelectContent>
                          <SelectItem value="有，自恢复">有，自恢复</SelectItem>
                          <SelectItem value="有，仅断电重启">有，仅断电重启</SelectItem>
                          <SelectItem value="无">无</SelectItem>
                        </SelectContent>
                      </Select>
                    </div>
                    
                    {formData.UNDERVOLTAGE_PROTECTION && formData.UNDERVOLTAGE_PROTECTION !== "无" && (
                      <div className="space-y-2 ml-4">
                        <Input
                          placeholder="保护值 (V)"
                          value={formData.UNDERVOLTAGE_VALUE}
                          onChange={(e) => handleInputChange("UNDERVOLTAGE_VALUE", e.target.value)}
                        />
                        <Input
                          placeholder="恢复值 (V)"
                          value={formData.UNDERVOLTAGE_RECOVERY_VALUE}
                          onChange={(e) => handleInputChange("UNDERVOLTAGE_RECOVERY_VALUE", e.target.value)}
                        />
                      </div>
                    )}
                  </div>

                  {/* 温度保护 */}
                  <div className="space-y-4">
                    <div className="space-y-2">
                      <Label className="text-sm flex items-center">
                        <Thermometer className="h-4 w-4 mr-1" />
                        温度保护
                      </Label>
                      <Select value={formData.TEMPERATURE_PROTECTION} onValueChange={(value) => handleInputChange("TEMPERATURE_PROTECTION", value)}>
                        <SelectTrigger>
                          <SelectValue placeholder="选择温度保护" />
                        </SelectTrigger>
                        <SelectContent>
                          <SelectItem value="有，自恢复">有，自恢复</SelectItem>
                          <SelectItem value="有，仅断电重启">有，仅断电重启</SelectItem>
                          <SelectItem value="无">无</SelectItem>
                        </SelectContent>
                      </Select>
                    </div>
                    
                    {formData.TEMPERATURE_PROTECTION && formData.TEMPERATURE_PROTECTION !== "无" && (
                      <div className="space-y-2 ml-4">
                        <Input
                          placeholder="保护温度 (°C)"
                          value={formData.protectionTemp}
                          onChange={(e) => handleInputChange("protectionTemp", e.target.value)}
                        />
                        <Input
                          placeholder="恢复温度 (°C)"
                          value={formData.recoveryTemp}
                          onChange={(e) => handleInputChange("recoveryTemp", e.target.value)}
                        />
                      </div>
                    )}
                  </div>

                  {/* 过流保护 */}
                  <div className="space-y-4">
                    <div className="space-y-2">
                      <Label className="text-sm flex items-center">
                        <Zap className="h-4 w-4 mr-1" />
                        过流保护
                      </Label>
                      <Select value={formData.OVERCURRENT_PROTECTION} onValueChange={(value) => handleInputChange("OVERCURRENT_PROTECTION", value)}>
                        <SelectTrigger>
                          <SelectValue placeholder="选择过流保护" />
                        </SelectTrigger>
                        <SelectContent>
                          <SelectItem value="有，自恢复">有，自恢复</SelectItem>
                          <SelectItem value="有，仅断电重启">有，仅断电重启</SelectItem>
                          <SelectItem value="无">无</SelectItem>
                        </SelectContent>
                      </Select>
                    </div>
                    
                    {formData.OVERCURRENT_PROTECTION && formData.OVERCURRENT_PROTECTION !== "无" && (
                      <div className="space-y-2 ml-4">
                        <Input
                          placeholder="过流值 (A)"
                          value={formData.overcurrentValue}
                          onChange={(e) => handleInputChange("overcurrentValue", e.target.value)}
                        />
                      </div>
                    )}
                  </div>

                  {/* 限流保护 */}
                  <div className="space-y-4">
                    <div className="space-y-2">
                      <Label className="text-sm flex items-center">
                        <Zap className="h-4 w-4 mr-1" />
                        限流保护
                      </Label>
                      <Select value={formData.CURRENT_LIMIT_PROTECTION} onValueChange={(value) => handleInputChange("CURRENT_LIMIT_PROTECTION", value)}>
                        <SelectTrigger>
                          <SelectValue placeholder="选择限流保护" />
                        </SelectTrigger>
                        <SelectContent>
                          <SelectItem value="有">有</SelectItem>
                          <SelectItem value="无">无</SelectItem>
                        </SelectContent>
                      </Select>
                    </div>
                    
                    {formData.CURRENT_LIMIT_PROTECTION && formData.CURRENT_LIMIT_PROTECTION !== "无" && (
                      <div className="space-y-2 ml-4">
                        <Input
                          placeholder="限流值 (A)"
                          value={formData.currentLimitValue}
                          onChange={(e) => handleInputChange("currentLimitValue", e.target.value)}
                        />
                      </div>
                    )}
                  </div>

                  {/* 堵孔保护 */}
                  <div className="space-y-4">
                    <div className="space-y-2">
                      <Label className="text-sm flex items-center">
                        <Zap className="h-4 w-4 mr-1" />
                        堵孔保护
                      </Label>
                      <Select value={formData.BLOCKAGE_PROTECTION} onValueChange={(value) => handleInputChange("BLOCKAGE_PROTECTION", value)}>
                        <SelectTrigger>
                          <SelectValue placeholder="选择堵孔保护" />
                        </SelectTrigger>
                        <SelectContent>
                          <SelectItem value="有，自恢复">有，自恢复</SelectItem>
                          <SelectItem value="有，仅断电重启">有，仅断电重启</SelectItem>
                          <SelectItem value="无">无</SelectItem>
                        </SelectContent>
                      </Select>
                    </div>
                    
                    {formData.BLOCKAGE_PROTECTION && formData.BLOCKAGE_PROTECTION !== "无" && (
                      <div className="space-y-2 ml-4">
                        <Input
                          placeholder="堵孔直径 (mm)"
                          value={formData.blockageDiameter}
                          onChange={(e) => handleInputChange("blockageDiameter", e.target.value)}
                        />
                        <Input
                          placeholder="堵孔时间 (S)"
                          value={formData.blockageHoldTime}
                          onChange={(e) => handleInputChange("blockageHoldTime", e.target.value)}
                        />
                      </div>
                    )}
                  </div>

                  {/* 缺相保护 */}
                  <div className="space-y-4">
                    <div className="space-y-2">
                      <Label className="text-sm flex items-center">
                        <Zap className="h-4 w-4 mr-1" />
                        缺相保护
                      </Label>
                      <Select value={formData.PHASE_PROTECTION} onValueChange={(value) => handleInputChange("PHASE_PROTECTION", value)}>
                        <SelectTrigger>
                          <SelectValue placeholder="选择缺相保护" />
                        </SelectTrigger>
                        <SelectContent>
                          <SelectItem value="有">有</SelectItem>
                          <SelectItem value="无">无</SelectItem>
                        </SelectContent>
                      </Select>
                    </div>
                  </div>
                </div>
              </CardContent>
            </Card>
          </TabsContent>
        </Tabs>
        
        <div className="flex justify-end gap-4">
          <Button type="button" variant="outline">
            取消
          </Button>
          <Button type="submit" disabled={loading}>
            {loading ? "创建中..." : "创建项目"}
          </Button>
        </div>
      </form>
    </div>
  )
}